package org.hikari.qqbot.xiuxian.task.impl;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.hikari.qqbot.xiuxian.command.ICommand;
import org.hikari.qqbot.xiuxian.command.standard.xiulian.DaZuoCommand;
import org.hikari.qqbot.xiuxian.command.standard.xiulian.XiShouLingLiCommand;
import org.hikari.qqbot.xiuxian.command.standard.xiulian.YiJianTiShengJingJieCommand;
import org.hikari.qqbot.xiuxian.service.bo.UserSetting;
import org.hikari.qqbot.xiuxian.task.TypedITask;
import org.hikari.qqbot.xiuxian.task.UserTaskRuntime;
import org.hikari.qqbot.xiuxian.util.BizUtil;
import org.hikari.qqbot.xiuxian.util.MessageUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * created at 2024-12-17 00:13:24
 *
 * @author yinchao
 * @version 1.0
 **/
@Slf4j
@Component
public class DaZuoTask extends TypedITask<DaZuoTask.Config> {

    @Getter
    @Setter
    @ToString
    public static class Config {

        // 打坐次数
        private int daZuoTimes = 1;
        // 自动提升境界
        private boolean autoLvlUp = true;
        // 聊天群id
        private Long chatGroupId;

    }

    @Override
    public String getId() {
        return "打坐";
    }

    @Override
    public ICommand initCommand(UserSetting setting) {
        // 初始化的时候发送一条[吸收灵力]
        return new XiShouLingLiCommand();
    }

    @Override
    public Collection<ICommand> process(UserTaskRuntime runtime, String message) {
        Collection<ICommand> commands = doProcess(runtime, message);
        if (commands == null || commands.isEmpty()) {
            return commands;
        }
        UserSetting setting = runtime.getUserSetting();
        Config config = config(setting);
        for (ICommand command : commands) {
            command.groupId(config.getChatGroupId());
        }
        return commands;
    }

    private List<ICommand> doProcess(UserTaskRuntime runtime, String message) {
        UserSetting setting = runtime.getUserSetting();
        Config config = this.config(setting);
        if (message.contains("吸收灵力成功")) {
            List<ICommand> commands = new ArrayList<>(2);
            if (this.config(setting).isAutoLvlUp() && message.contains("可以提升境界了") && !message.contains("金丹不足")) {
                commands.add(new YiJianTiShengJingJieCommand());
            }
            commands.add(new DaZuoCommand().times(config.getDaZuoTimes()));
            return commands;
        }

        if (message.contains("你还没有打坐！")) {
            return List.of(new DaZuoCommand().times(config.getDaZuoTimes()));
        }

        if (message.contains("打坐中")) {
            Date sendTime = MessageUtil.parseExpectTime(message);
            if (sendTime != null) {
                return List.of(new XiShouLingLiCommand().delay(sendTime));
            }
        }

        // 洞府等级不够，进行判断
        if (message.contains("需要洞府等吉达到")) {
            // 重新设置打坐次数
            config.setDaZuoTimes(10);
            log.info("洞府等级不够，强制设置打坐次数为10");
            return List.of(new DaZuoCommand().times(config.getDaZuoTimes()));
        }

        // 体力次数不足时，进行判断
        if (message.contains("需要消耗次数")) {
            Integer tiLiTimes = MessageUtil.parseTiLiTimes(message);
            if (tiLiTimes != null) {
                // 没有体力的时候，明天继续
                if (tiLiTimes == 0) {
                    return List.of(new DaZuoCommand().times(config.getDaZuoTimes()).delay(BizUtil.beginOfTomorrow()));
                }
                // 降级为10连
                else if (tiLiTimes >= 10) {
                    return List.of(new DaZuoCommand().times(10));
                }
                // 降级为单次打坐
                else {
                    return List.of(new DaZuoCommand());
                }
            }
        }
        return null;
    }

}
